package com.pzj.dormitory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pzj.dormitory.entity.Dormitory;
import com.pzj.dormitory.form.RuleForm;
import com.pzj.dormitory.form.SearchForm;
import com.pzj.dormitory.service.impl.DormitoryServiceImpl;
import com.pzj.dormitory.util.ResultVOUtil;
import com.pzj.dormitory.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-04-28
 */
@RestController
@RequestMapping("/dormitory")
public class DormitoryController {

    @Autowired
    private DormitoryServiceImpl dormitoryService;



    @GetMapping("/availableList")
    public ResultVO<Dormitory> availableList(){
        QueryWrapper<Dormitory> dormitoryQueryWrapper = new QueryWrapper<>();
        dormitoryQueryWrapper.gt("available",0);
        List<Dormitory> list = dormitoryService.list(dormitoryQueryWrapper);
        return ResultVOUtil.success(list);
    }

    @PostMapping("/save")
    public  ResultVO save(@RequestBody Dormitory dormitory){
        dormitory.setAvailable(dormitory.getType());
        return ResultVOUtil.success(dormitoryService.save(dormitory));
    }

    @GetMapping("/list/{pageNum}/{pageSize}")
    public ResultVO list(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize){
        return ResultVOUtil.success(dormitoryService.list(pageNum,pageSize));
    }

    @GetMapping("/search")
    public ResultVO search(SearchForm searchForm){
        System.out.println("searchForm = " + searchForm);
        return ResultVOUtil.success(dormitoryService.search(searchForm));
    }

    @GetMapping("/findById/{id}")
    public  ResultVO findById(@PathVariable("id") Integer id){
        return ResultVOUtil.success(dormitoryService.getById(id));
    }

    @PutMapping("/update")
    public ResultVO update (@RequestBody Dormitory dormitory){
        return ResultVOUtil.success(dormitoryService.updateById(dormitory));
    }
    @DeleteMapping("/deleteById/{id}")
    public ResultVO deleteById(@PathVariable("id") Integer id){
        return ResultVOUtil.success(dormitoryService.deleteById(id));
    }
}

